const express = require("express")
const {con,getMysql} = require('../../mysql/mysql.js'); 
const article = express.Router() //创建路由

//获取全部评论数据
article.get('/getallcomment',async (req,res)=>{
    let {articleid}=req.query
    let str=`select * from commentlist where articleid=${articleid}`
    let data=await getMysql(str)

    // console.log(data)
    let idlist=data.map(item=>{
        return item.readerid
    })

    // let arr1=[]
    for(let i=0;i<idlist.length;i++){
        let str=`select * from user where id=${idlist[i]}`
        let data2=await getMysql(str)
        data[i]['author']=data2[0].author
        data[i]['headerimg']=data2[0].headerimg
        // arr1.push(data2[0])
        
    }
    // console.log(data)
    // console.log(idlist)
    res.send({status:200,data:data})
})


//提交评论接口
article.post("/putcomment",async (req,res)=>{
    let {articleid,readerid,comment}=req.body
    let str="insert into commentlist (articleid,readerid,comment) values (?,?,?)"
    let arr=[articleid,readerid,comment]
    let data=await getMysql(str,arr)
    res.send({status:200,data:data})
    })

//文章收藏量
article.get("/getCollectionNumber",async (req,res)=>{
let {articleid}=req.query
let str=`select * from collection where articleid=${articleid}`
let data=await getMysql(str)
// console.log(data.length)
res.send({status:200,number:data.length})
})

//提供所有历史记录接口
article.get("/allarticlehistory",async (req,res)=>{
    let {readerid}=req.query
    let str=`select * from history where readerid=${readerid}`
    let data=await getMysql(str)
    let arr=[]
    let arr1=[]
    arr=data.map((item)=>{
        return item.articleid
    })
    arrDate=data.map((item)=>{
        return item.pushdate
    })
    // res.send(arr)
    for(let i=0;i<arr.length;i++){
        let str1=`select * from  article where articleid=${arr[i]}`
        let data1=await getMysql(str1)
        data1[0]['pushdate']=arrDate[i]
        arr1.push(data1[0])  
    }
    
    res.send(arr1)
})


//对浏览后的文章收藏进历史记录中
article.post("/historyarticle",async (req,res)=>{
    let {articleid,chapterid,readerid}=req.body
    // 判断是否浏览过
    let str1=`select * from history where articleid=${articleid} and readerid=${readerid}`
    let data1=await getMysql(str1)
    if(data1.length>0){
        //浏览过
        let str=`update history set articleid=${articleid},chapterid=${chapterid},readerid=${readerid} where articleid=${articleid} and readerid=${readerid} `
        let data=await getMysql(str)
        return res.send("历史记录跟新成功")
    }
    //没有被收藏
    let str2="insert into history (articleid,chapterid,readerid) values (?,?,?)"
    let arr2=[articleid,chapterid,readerid]
    let data2=await getMysql(str2,arr2)
    return res.send({message:"历史记录新加一条"})
})

//浏览收藏的文章，直接具有历史记录功能，每一个页面都会自动发送该请求
article.post("/collectarticle2",async (req,res)=>{
    let {articleid,chapterid,readerid}=req.body
    // 判断是否收藏
    let str1=`select * from collection where articleid=${articleid} and readerid=${readerid}`
    let data1=await getMysql(str1)
    if(data1.length>0){
        //是收藏的
        let str=`update collection set articleid=${articleid},chapterid=${chapterid},readerid=${readerid} where articleid=${articleid} and readerid=${readerid}`
        let data=await getMysql(str)
        return res.send("收藏并跟新成功")
    }
    //没有被收藏
    return res.send("文章没有被收藏")
})

//删除对应收藏
article.get("/delcollection",async (req,res)=>{
    let {readerid,articleid}=req.query
    let str=`delete from collection where readerid=${readerid} and articleid=${articleid}`
    let data=await getMysql(str)
    res.send({status:200,message:"删除成功",data:data})
})

//删除对应历史记录
article.get("/delhistory",async (req,res)=>{
    let {readerid,articleid}=req.query
    let str=`delete from history where readerid=${readerid} and articleid=${articleid}`
    let data=await getMysql(str)
    res.send({status:200,message:"删除成功",data:data})
})

//从收藏页面直接过去,返回chapterid
article.get("/chapterid",async (req,res)=>{
    // res.send(req.query)
    let {readerid,articleid}=req.query
    let str=`select * from collection where readerid=${readerid} and articleid=${articleid}`
    let data=await getMysql(str)
    res.send({status:200,message:"提交成功",data:data})
})

//从历史页面直接过去,返回chapterid
article.get("/chapterid1",async (req,res)=>{
    let {readerid,articleid}=req.query
    let str=`select * from history where readerid=${readerid} and articleid=${articleid}`
    let data=await getMysql(str)
    res.send({status:200,message:"提交成功",data:data})
})


//页面内主动收藏文章，并保存当前的浏览记录
article.post("/collectarticle1",async (req,res)=>{
    let {articleid,chapterid,readerid}=req.body
    let str1=`select * from collection where articleid=${articleid} and readerid=${readerid}`
    let data1=await getMysql(str1)
    if(data1.length>0){
        let str=`update collection set articleid=${articleid},chapterid=${chapterid},readerid=${readerid} where articleid=${articleid} and readerid=${readerid}`
        let data=await getMysql(str)
        res.send("收藏并跟新成功")
        return
    }
    let arr=[articleid,chapterid,readerid]
    let str="insert into collection (articleid,chapterid,readerid) values (?,?,?)"
    let data=await getMysql(str,arr)
    res.send({status:200,message:"提交成功",data:data})
})



//获取所有收藏的文章
article.get("/allarticlecollection",async (req,res)=>{
    let {readerid}=req.query
    // console.log(readerid)
    let str=`select * from collection where readerid=${readerid}`
    let data=await getMysql(str)
    console.log(data)
    let arr=[]
    let arr1=[]
    arr=data.map((item)=>{
        return item.articleid
    })
    arrDate=data.map((item)=>{
        return item.pushdate
    })
    for(let i=0;i<arr.length;i++){
        let str1=`select * from article where articleid=${arr[i]}`
        let data1=await getMysql(str1)
        // console.log(data1[0])
        data1[0]['pushdate']=arrDate[i]
        arr1.push(data1[0])
    }
    res.send(arr1)
})

//提交新的章节
article.post("/newarticle",async (req,res)=>{
    let {articleid,chapterid,chaptertitle,chaptercontent,pushdate}=req.body
    let arr=[articleid,chapterid,chaptertitle,chaptercontent,pushdate]
    let str=`insert into chapter (articleid,chapterid,chaptertitle,chaptercontent,pushdate) values (?,?,?,?,?)`
    let data=await getMysql(str,arr)
    res.send({status:200,message:"提交成功",data:data})

})

//搜索自己的作品
article.get("/articlesearch",async (req,res)=>{
    let {message}=req.query
    let str=`select * from article where  bookname like '%${message}%'`
    let data=await getMysql(str)
    res.send({status:200,message:"查找成功",data:data})

})

//根据id获取文章详情
article.get("/getonearticle",async (req,res)=>{
    let {articleid}=req.query
    let str=`select * from article where articleid=${articleid}`
    let data=await getMysql(str)
    res.send({status:200,message:"成功",data:data})
})

//根据id删除文章
article.get("/delbook",async (req,res)=>{
    let {articleid}=req.query
    let str=`delete from article where articleid=${articleid}`
    let data=await getMysql(str)
    res.send({status:200,message:"删除成功",data:data})
})

//修改一本书
article.post("/xiugaibook",async (req,res)=>{
    // res.send(req.body)
    let {articleid,authorid,bookname,introduction,imgurl}=req.body
    let str="update article set authorid=?,bookname=?,introduction=?,imgurl=? where articleid=?"
    let arr=[authorid,bookname,introduction,imgurl,articleid]
    let data=await getMysql(str,arr)
    res.send({status:200,message:"修改成功",data:data})
})

//创建一本新书
article.post("/newbook",async (req,res)=>{
    let {authorid,bookname,introduction,imgurl}=req.body

    let str=`insert into article (authorid,bookname,introduction,imgurl) values (?,?,?,?)`
    let arr=[authorid,bookname,introduction,imgurl]
    let data=await getMysql(str,arr)
    res.send({status:200,message:"修改成功",data:data})
})

//获取全部的个人作品
article.get("/",async (req,res)=>{
    let {authorid}=req.query
    let str=`select * from article where authorid=${authorid}`
    let data=await getMysql(str)
    res.send({status:200,message:"查找成功",data:data})
})

//搜索自己的作品
article.get("/book",async (req,res)=>{
    let {authorid,message}=req.query
    let str=`select * from article where bookname like '%${message}%' and authorid=${authorid}`
    let data=await getMysql(str)
    res.send({status:200,message:"查找成功",data:data})

})


module.exports = article

